package homework;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientLiu {
    private Socket socket;
    public ClientLiu() {
        try {
            System.out.println("客户端链接中。。。");
            socket = new Socket("localhost", 8488);
            System.out.println("客户端连接成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        try {
            OutputStream out = socket.getOutputStream();
            while (true) {
                Scanner sc = new Scanner(System.in);
                String line = sc.nextLine();
                if("exit".equals(line)){
                    break;
                }
                byte[] datas = line.getBytes(java.nio.charset.StandardCharsets.UTF_8);
                out.write(datas.length);
                out.write(datas);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(socket != null){
                   socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        ClientLiu clientLiu = new ClientLiu();
        clientLiu.start();
    }
}
